/**
 * 
 */
package com.base.myproject.client.busi.b2c;

import java.util.Iterator;
import java.util.Set;

import com.base.myproject.client.busi.b2c.b2cgcodeattr.AlsoBuy;
import com.base.myproject.client.busi.b2c.b2cgcodeattr.B2cGcodeAttr;
import com.base.myproject.client.busi.b2c.shopping.ShoppingCar;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.tools.XML;
import com.base.myproject.client.ui.BaseTabPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

/**
 * @author zgw 2009-7-15 下午04:02:00 javazgw@gmail.com
 */
public class OneProduct extends ContentPanel {
	ProdectDesc prodectdesc = null;
	BaseTabPanel btp = BaseTabPanel.getInstance();
	Image mainimage = null;
	VerticalPanel vpanel = new VerticalPanel();
	public OneProduct(ProdectDesc prodectdesc) {
		this.prodectdesc = prodectdesc;
		init();

	}

	void init() {
		//this.setStylePrimaryName("base-panel");
		B2cgcodeYouSaw.getinstance().add(prodectdesc.getId());
		MainPage.getToppanelleft().removeAll();
		String mainpage = "<a href=\"javascript:void(0)\" onclick=\"javascript:_topnavigationmainpage()\">首页</a>";
		String grouppage = "<a href=\"javascript:void(0)\" onclick=\"javascript:_topnavigationgrouppage('"
				+ prodectdesc.getGcode()
				+ "')\">"
				+ prodectdesc.getGname()
				+ "</a>";

		String h = mainpage
				+ "--"
				+ grouppage
				+ "--"
				+ "<a href=\"javascript:void(0)\" onclick=\"javascript:_topnavigation('"
				+ prodectdesc.getTreecode() + "')\">"
				+ prodectdesc.getTreename() + "</a>";

		MainPage.getToppanelleft().add(new Html(h));
		MainPage.getToppanelleft().layout();
		btp.removeAll(); // 清除原来的。。
		btp.setWidth(720);
		TableLayout tl = new TableLayout(2);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setColspan(1);

		ColumnLayout cl = new ColumnLayout();
		setLayout(cl);
		this.setHeaderVisible(false);
		this.setBodyBorder(false);

		LayoutContainer layoutcontainer = new LayoutContainer();
		
		layoutcontainer.setLayout(new TableLayout(2));
		TableData td1 = new TableData();
		td1.setRowspan(1);
		td1.setColspan(1);
		td1.setVerticalAlign(VerticalAlignment.TOP);
		td1.setHorizontalAlign(HorizontalAlignment.LEFT);
		layoutcontainer.add(CreateImage(), td1);
		layoutcontainer.add(Createb2cgcodeDesc(), td1);
		TabItem tabitem = new TabItem();
		tabitem.add(layoutcontainer);
		tabitem.setText("概览");
		btp.add(tabitem);

		// add(Createb2cgcodeDesc());
		// add(Createb2cgcodeAttr());
		CreateAtrr();
		add(btp);
		AlsoBuy ab = new AlsoBuy(prodectdesc.getId());
		
		vpanel.add(ab.getWidget());
		add(vpanel);
		setWidth(720);
	}

	/**
	 * @return the mainimage
	 */
	public Image getMainimage() {
		return mainimage;
	}

	/**
	 * @param mainimage the mainimage to set
	 */
	public void setMainimage(Image mainimage) {
		this.mainimage = mainimage;
	}

	/**
	 * 产品的描述
	 * 
	 * @return
	 */
	public ProdectDesc getProdectdesc() {
		return prodectdesc;
	}

	public void setProdectdesc(ProdectDesc prodectdesc) {
		this.prodectdesc = prodectdesc;
	}

	VerticalPanel CreateImage() {

		VerticalPanel imagecontainer = new VerticalPanel();
		mainimage = new Image();
		// image.setUrl("busiimages/20090714/1247575528765Winter.jpg");
		mainimage.setUrl(prodectdesc.getMainimagepath());
		mainimage.setWidth("320px");
		mainimage.setHeight("240px");
	
		mainimage.setStylePrimaryName("base-Image");
//		mainimage.add
		// RowLayout rowlayout = new RowLayout();
		// imagecontainer.setLayout(rowlayout);
		imagecontainer.add(mainimage);

		imagecontainer.add(CreateImage5pic());
		imagecontainer.setWidth(300);
		imagecontainer.setHeight(300);
		return imagecontainer;
	}

	/**
	 * 最多显示5张图片
	 * 
	 * @return
	 */
	LayoutContainer CreateImage5pic() {
		LayoutContainer imagecontainer = new LayoutContainer();
		
		
		ColumnLayout columnlayout = new ColumnLayout();

		String sql = "select top 5 imagepath from b2cgcodeimage where b2cgcode='"
				+ prodectdesc.getId() + "'";

		SendServlet ss = new SendServlet();
		String datawinstr = ss.getStringx("/"+GWT.getModuleName()+"/BaseServlet",
				"Action=getdatasql&sql=" + sql + "");
		Document doc = XMLParser.parse((String) datawinstr);

		NodeList list = doc.getElementsByTagName("record");

		for (int i = 0; i < list.getLength(); i++) {
			NodeList recordlist = list.item(i).getChildNodes();

			Node node = list.item(i);
			Element elem = (Element) node;

			final Image image = new Image();
			image.setStylePrimaryName("base-min-Image");
			// image.setUrl(prodectdesc.getMainimagepath());
			image.setUrl(XML.getValue(elem, "imagepath"));
			// image.setHeight("60px");
			// image.setWidth("60px");
			image.addStyleName("imagesplit");

			image.addMouseMoveHandler(new MouseMoveHandler() {

				@Override
				public void onMouseMove(MouseMoveEvent event) {
					if (!image.getUrl().equals(getMainimage().getUrl())) {
						getMainimage().setUrl(image.getUrl());
						
					}
				}

			});
			imagecontainer.add(image);

		}

		imagecontainer.setLayout(columnlayout);
		// for (int i = 0; i < 5; i++) {
		//			
		//	
		// }
		imagecontainer.setWidth(300);
		imagecontainer.setHeight(100);

		return imagecontainer;
	}

	/**
	 * 商品的简单描述
	 * 
	 * @return
	 */
	LayoutContainer Createb2cgcodeDesc() {
		String listyle ="style=\"padding:5px\"";
		LayoutContainer desc = new LayoutContainer();
		TableLayout tl = new TableLayout(2);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setColspan(1);
		desc.setLayout(tl);

		VerticalPanel vpanel = new VerticalPanel();
		vpanel.setWidth(200);
		Html html = new Html();

		String sql = "select top 5 attrname,attrvalue from b2cgcodeattrg where b2cgcode='"
				+ prodectdesc.getId() + "'";

		SendServlet ss = new SendServlet();
		String datawinstr = ss.getStringx("/"+GWT.getModuleName()+"/BaseServlet",
				"Action=getdatasql&sql=" + sql + "");
		Document doc = XMLParser.parse((String) datawinstr);

		NodeList list = doc.getElementsByTagName("record");

		 String listr = "";
		for (int i = 0; i < list.getLength(); i++) {
			NodeList recordlist = list.item(i).getChildNodes();

			Node node = list.item(i);
			Element elem = (Element) node;

			Html htmlattr = new Html();
			htmlattr.setHtml(XML.getValue(elem, "attrname") + ":");

			Html htmlattrvalue = new Html();
			htmlattrvalue.setHtml(XML.getValue(elem, "attrvalue"));
//			desc.add(htmlattr, td);
//			desc.add(htmlattrvalue, td);

			if(XML.getValue(elem, "attrvalue")!=null)
			 
				{listr +="<li "+listyle+">"+XML.getValue(elem, "attrname")+":";
			 listr +=XML.getValue(elem, "attrvalue")+"</li>";
				}
		}

		html.setHtml("<a style=\" background-color:#FFCC9;\">"
				+ getProdectdesc().getPrice() + "");
		String htmlstr = "";
		htmlstr += "<div class=\"productatrr\" style=\"padding:10px;border:1px solid #F0E1C4;background:#fff3d9;width:300px;height:80px;font:12px/1.5 Tahoma, Helvetica, Arial, sans-serif;\">";
		htmlstr += "<ul style=\"margin:0;padding:0;list-style:none\">";
		htmlstr += "<li "+listyle+">";
		htmlstr += "产品编号：" + getProdectdesc().getId();
		htmlstr += "</li>";

		if(!(getProdectdesc().getDiscountprice()==null || getProdectdesc().getDiscountprice().equals("")))
		{	
			htmlstr += "<li style=\"padding:5px;text-decoration:line-through\">";
			htmlstr += "价格：" + getProdectdesc().getPrice() + "元";
			htmlstr += "</li>";
			
			htmlstr += "<li "+listyle+">";
			htmlstr += "折扣价："+getProdectdesc().getDiscountprice() + "元";
			htmlstr += "</li>";
		}
		else
		{
			htmlstr += "<li style=\"padding:5px\">";
			htmlstr += "价格：" + getProdectdesc().getPrice() + "元";
			htmlstr += "</li>";
		}
		htmlstr += "<li "+listyle+">";
		htmlstr += "上市日期：2007年";
		htmlstr += "</li>";
		htmlstr += "<li "+listyle+">";
		htmlstr += "品牌：飞利浦";
		htmlstr += "</li>";
		 htmlstr +=listr;
		htmlstr += "</ul>";
		htmlstr += "</div>";
		html.setHtml(htmlstr);
		//html.setStylePrimaryName("base-Image");
		// Image image = new Image("images/b2cimages/shopcar.gif");
		// image.

		
		Button addcar = new Button("添加到购物车");

		addcar.setIconStyle("shoppingcar");

		Label l = new Label ("dddddddd");
		//l.setStylePrimaryName("delControl");
		
		addcar.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				System.out.println("Env.getUid()=" + (Env.getUid() == null));
				if (Env.getUid() == null) {
					B2cLogin b2clogin = new B2cLogin() {
						protected boolean login() {
							if (super.login()) {
								ShoppingCar shopingcar = new ShoppingCar();
								shopingcar.readcarInStore(Env.getEnvname());
								shopingcar.add2Car(getProdectdesc());

								return true;
							} else
								return false;
						}
					};

					Dialog dialog = b2clogin.CreateLoginDialog();

					dialog.show();
				} else {
					ShoppingCar shopingcar = new ShoppingCar();
					shopingcar.add2Car(getProdectdesc());

				}

			}

		});
		vpanel.add(html);
		// vpanel.add(image);
		vpanel.add(addcar);
	//	vpanel.add(l);
		vpanel.setStyleAttribute("margin", "10px");
		desc.add(vpanel);

		return desc;

	}

	LayoutContainer Createb2cgcodeAttr() {
		System.out.println("ddd");
		LayoutContainer attr = new LayoutContainer();
		int size = getProdectdesc().getGcodeattrmap().size();
		Set key = getProdectdesc().getGcodeattrmap().keySet();
		Iterator iterator = key.iterator();
		while (iterator.hasNext()) {
			String keystr = iterator.next().toString();

			attr
					.add(getProdectdesc().getGcodeattrmap().get(keystr)
							.getWidget());
		}

		return attr;
	}

	public void CreateAtrr() {

		Set keyset = prodectdesc.getGcodeattrmap().keySet();
		Iterator iterator = keyset.iterator();
		while (iterator.hasNext()) {
			String s = iterator.next().toString();
			final B2cGcodeAttr w = (B2cGcodeAttr) prodectdesc.getGcodeattrmap()
					.get(s);
			TabItem tabitem = new TabItem();
			tabitem.add(w.getWidget());
			tabitem.setText(w.getName());
			tabitem.addListener(Events.Select, new Listener() {

				@Override
				public void handleEvent(BaseEvent be) {
					w.setActive(true);

				}

			});
			btp.add(tabitem);

		}

	}
}
